package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.anno.AuthLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;


//     /api/payment/wxPay/createJsapi/1301/2d8f35f693ff4f81903527e43b4f16b4  POST
    @AuthLogin
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result<WeChatPayVo> createJsapi(@PathVariable String paymentType,@PathVariable String orderNo){
        return Result.ok(wxPayService.createJsapi(paymentType,orderNo));
    }
//     /api/payment/wxPay/queryPayStatus/160fe55b85b04904ac752a3e1eae5135
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo){
        return Result.ok(wxPayService.queryPayStatus(orderNo));
    }
// /api/payment/wxPay/notify
    @PostMapping("notify")
    public ResponseEntity<Map<String,String>> notificationFunc(HttpServletRequest httpServletRequest){
        Map<String,String> map = new HashMap<>();
        try {
            wxPayService.notificationFunc(httpServletRequest);
            map.put("code","SUCCESS");
            map.put("message","成功");
            return ResponseEntity.status(HttpStatus.OK).body(map);
        }catch (Exception e){
            map.put("code","FAIL");
            map.put("message","失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
        }

    }
}
